//
//  FrameType.m
//  FrameController
//
//  Created by Le Thanh Hai on 9/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "FrameView.h"

@implementation FrameView

@synthesize scrollView, scrollViewType;

const NSUInteger kNumImages		= 11;
const NSUInteger kNumTypes		= 7;


- (void)layoutScrollImagesWithScrollView:(UIScrollView*)scroll Row:(NSInteger)row Colum:(NSInteger)colum andImages:(NSInteger)images
{
	UIView *view = nil;
	NSArray *subviews = [scroll subviews];
    float heght = scroll.bounds.size.height / row;
    float width = scroll.bounds.size.width  / colum;

	// reposition all image subviews in a horizontal serial fashion
	CGFloat curXLoc = 0;
    CGFloat curYLoc = 0;
    NSInteger index = 0;
	for (view in subviews)
	{
		if ([view isKindOfClass:[UIView class]] && view.tag > 0)
		{
            index ++;
			CGRect frame = view.frame;
			frame.origin = CGPointMake(curXLoc, curYLoc);
            frame.size = CGSizeMake(width, heght);
			view.frame = frame;
            curYLoc += heght;
            
			if ((index % row) == 0) {
                curYLoc = 0;
                curXLoc += width;
            }
		}
	}	
	// set the content size so it can be scrollable   
	[scroll setContentSize:CGSizeMake(( ((char)(images/(row*colum))+1) * [scroll bounds].size.width), [scroll bounds].size.height)];
}

- (void)viewDidLoad
{ 
    // Add content view 
    contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 360)];
    contentView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
    [self.view addSubview:contentView];
    [contentView release];    
    
    // Add image background for scroll frame
    UIImageView *imgBackground = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.png"]] autorelease];
    imgBackground.frame = CGRectMake(0, 0, 320, 295);
    [contentView addSubview:imgBackground];
    
    // Add label フレームツール
    UILabel *label = [[[UILabel alloc] init] autorelease];
    label.frame = CGRectMake(0, 337, 109, 24);
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont fontWithName:@"AppleGothic" size:12];
    label.textAlignment = UITextAlignmentCenter;
    label.text = @"フレームツール";
    [contentView addSubview:label];
    
    // Add button back
    UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
    btnBack.frame = CGRectMake(278, 295, 40, 40);
    [btnBack setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    btnBack.backgroundColor = [UIColor clearColor];
    [btnBack setTitle:@"" forState:UIControlStateNormal];
    [btnBack addTarget:self action:@selector(backtoMainView:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:btnBack];
    
	// Setup the scrollview for multiple images and add it to the view controller
	//
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(30, 15, 265, 270)];
    [contentView addSubview:scrollView];
    
	[scrollView setBackgroundColor:[UIColor clearColor]];
	[scrollView setCanCancelContentTouches:NO];
	scrollView.indicatorStyle = UIScrollViewIndicatorStyleDefault;
	scrollView.clipsToBounds = YES;		// default is NO, we want to restrict drawing within our scrollview
	scrollView.scrollEnabled = YES;
	
	// pagingEnabled property default is NO, if set the scroller will stop or snap at each photo
	// if you want free-flowing scroll, don't set this property.
	scrollView.pagingEnabled = YES;
	
	// load all the images from our bundle and add them to the scroll view
	NSUInteger i;
	for (i = 1; i <= kNumTypes; i++)
	{
        Frame *frame = [[Frame alloc] initWithFrameName:[NSString stringWithFormat:@"style1_%d", i]];
		UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"style1_%d.png", i]]];
        CGRect rect = imageView.frame;
        rect.size = CGSizeMake(88.33f, 135);
        imageView.frame = rect;
        [frame addSubview:imageView];
		frame.tag = i;	// tag our images for later use when we place them in serial fashion
        frame.delegate = self;
		[scrollView addSubview:frame];
		[imageView release];
        [frame release];
	}
	
	[self layoutScrollImagesWithScrollView:scrollView Row:2 Colum:3 andImages:kNumImages];	// now place the photos in serial layout within the scrollview
    
    
    // Setup the Scroll View Type for multiple images and add it to the view controller
	//
    scrollViewType = [[UIScrollView alloc] initWithFrame:CGRectMake(5, 295, 270, 35)];
    [contentView addSubview:scrollViewType];

	[scrollViewType setBackgroundColor:[UIColor clearColor]];
	[scrollViewType setCanCancelContentTouches:NO];
	scrollViewType.indicatorStyle = UIScrollViewIndicatorStyleDefault;
	scrollViewType.clipsToBounds = YES;		// default is NO, we want to restrict drawing within our scrollview
	scrollViewType.scrollEnabled = YES;
	
	// pagingEnabled property default is NO, if set the scroller will stop or snap at each photo
	// if you want free-flowing scroll, don't set this property.
	scrollViewType.pagingEnabled = YES;
	
	// load all the images from our bundle and add them to the scroll view
	for (i = 1; i <= kNumTypes; i++)
	{
        FrameType *frameType = [[FrameType alloc] initWithTypeName:[NSString stringWithFormat:@"style%d.jpg", i]];
		UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"style%d.jpg", i]]];	
        [frameType addSubview:imageView];
		frameType.tag = i;	// tag our images for later use when we place them in serial fashion
        frameType.delegate = self;
		[scrollViewType addSubview:frameType];
		[imageView release];
        [frameType release];
	}
	
	[self layoutScrollImagesWithScrollView:scrollViewType Row:1 Colum:5 andImages:kNumTypes];	// now place the photos in serial layout within the scrollview
}

-(void)TapWasFrameType:(NSString *)frameType
{
    NSString *slideStyle = [frameType substringToIndex:[frameType length]-4];
    [scrollView removeFromSuperview];
    [scrollView release];
    // Setup the scrollview for multiple images and add it to the view controller
	//
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(30, 15, 265, 270)];
    [contentView addSubview:scrollView];
    
	[scrollView setBackgroundColor:[UIColor clearColor]];
	[scrollView setCanCancelContentTouches:NO];
	scrollView.indicatorStyle = UIScrollViewIndicatorStyleDefault;
	scrollView.clipsToBounds = YES;		// default is NO, we want to restrict drawing within our scrollview
	scrollView.scrollEnabled = YES;
	
	// pagingEnabled property default is NO, if set the scroller will stop or snap at each photo
	// if you want free-flowing scroll, don't set this property.
	scrollView.pagingEnabled = YES;
	
	// load all the images from our bundle and add them to the scroll view
	NSUInteger i;
	for (i = 1; i <= kNumImages; i++)
	{
        Frame *frame = [[Frame alloc] initWithFrameName:[NSString stringWithFormat:@"%@_%d", slideStyle, i]];
		UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_%d.png", slideStyle, i]]];	
        CGRect rect = imageView.frame;
        rect.size = CGSizeMake(88.33f, 135);
        imageView.frame = rect;
        [frame addSubview:imageView];
		frame.tag = i;	// tag our images for later use when we place them in serial fashion
        frame.delegate = self;
		[scrollView addSubview:frame];
		[imageView release];
        [frame release];
	}
	
	[self layoutScrollImagesWithScrollView:scrollView Row:2 Colum:3 andImages:kNumImages];	// now place the photos in serial layout within the scrollview
}

-(void)frameWasTap:(NSString *)frame
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"addFrameToView" object:frame];
}

-(void)backtoMainView:(id)sender
{
	[[NSNotificationCenter defaultCenter] postNotificationName:@"HideViewMenu" object:@"HideView"];
}

- (void)dealloc
{	
	[scrollView release];	
	[super dealloc];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    // return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return YES;
}

- (void)didReceiveMemoryWarning
{
	// invoke super's implementation to do the Right Thing, but also release the input controller since we can do that	
	// In practice this is unlikely to be used in this application, and it would be of little benefit,
	// but the principle is the important thing.
	//
	[super didReceiveMemoryWarning];
}

@end

